package com.lanyou.esb.cook.proxy.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.lanyou.esb.cook.proxy.entity.IfAlarmWorkOrder;

/**
 * 接口告警工单持久层
 * 
 * @author Davey.wu
 */
public class IfAlarmWorkOrderDao extends BaseDao<IfAlarmWorkOrder> {
	private static final List<String> FIELDS = new ArrayList<String>();
	/** SQL **/
	private static final String SQL;
	static {
		FIELDS.add("WO_ID");
		FIELDS.add("ASSIGN_WORK_CODE");
		FIELDS.add("WO_STATUS");
		FIELDS.add("ASSIGN_DATE");
		FIELDS.add("IF_ID");
		FIELDS.add("ERROR_ANALYSIS");
		FIELDS.add("DEAL_DATE");
		FIELDS.add("DEAL_PERSON");
		FIELDS.add("EMIAL_RECEIVER");
		FIELDS.add("IS_ENABLE");
		FIELDS.add("CREATOR");
		FIELDS.add("CREATED_DATE");
		FIELDS.add("MODIFIER");
		FIELDS.add("LAST_UPDATED_DATE");
	}
	static {
		StringBuilder builderFields = new StringBuilder();
		StringBuilder builderValues = new StringBuilder();
		builderFields.append(" (");
		builderValues.append(" (");
		int size = FIELDS.size();
		for (int i = 0; i < size; ++i) {
			builderFields.append(FIELDS.get(i) + (i == size - 1 ? "" : ","));
			builderValues.append((i == size - 1 ? "?" : "?,"));
		}
		builderFields.append(") ");
		builderValues.append(") ");
		SQL = "insert into t_esb_bu_if_alarm_wo" + builderFields.toString()
				+ " values " + builderValues.toString();
	}

	@Override
	public int save(IfAlarmWorkOrder workOrder) {
		return getJdbcTemplate().update(SQL, workOrder.getId(),
				workOrder.getCode(), workOrder.getStatus(),
				workOrder.getAssignDate(), workOrder.getIfId(),
				workOrder.getAnalysis(), workOrder.getDealDate(),
				workOrder.getDealPerson(), workOrder.getEmailReceiver(),
				workOrder.getIsEnable(), workOrder.getCreator(),
				workOrder.getCreateDate(), workOrder.getModifier(),
				workOrder.getLastUpDate());
	}

	@Override
	public int saveAll(List<IfAlarmWorkOrder> list) {
		if (list == null || list.isEmpty()) {
			return 0;
		}
		List<Object[]> args = new ArrayList<>();
		int i = 0;
		for (IfAlarmWorkOrder workOrder : list) {
			i = 0;
			Object[] objects = new Object[FIELDS.size()];
			objects[i++] = workOrder.getId();
			objects[i++] = workOrder.getCode();
			objects[i++] = workOrder.getStatus();
			objects[i++] = workOrder.getAssignDate();
			objects[i++] = workOrder.getIfId();
			objects[i++] = workOrder.getAnalysis();
			objects[i++] = workOrder.getDealDate();
			objects[i++] = workOrder.getDealPerson();
			objects[i++] = workOrder.getEmailReceiver();
			objects[i++] = workOrder.getIsEnable();
			objects[i++] = workOrder.getCreator();
			objects[i++] = workOrder.getCreateDate();
			objects[i++] = workOrder.getModifier();
			objects[i++] = workOrder.getLastUpDate();
			args.add(objects);
		}
		int[] result = getJdbcTemplate().batchUpdate(SQL, args);
		int num = 0;
		for (int k : result) {
			num += k;
		}
		return num;
	}
}
